<?php

namespace app\common\model\mysql;

use think\Model;

class Category extends BaseModel
{


    //查询状态正常的 所有字段
    public function getNormalCategorys($field = "*")
    {

        $where = [
            "status" => 1,

        ];
        $order = [
            "listorder" => "desc",
            "id" => "desc"
        ];
        $result = $this
            ->where($where)
            ->field($field)
            ->order($order)
            ->select();
        return $result;
    }

    //分页
    public function getLists($where, $num = 10)
    {
        $order = [
            "listorder" => "desc",
            "id" => "desc"
        ];

        $result = $this->where("status", "<>", "99")
            ->where($where)
            ->order($order)
            ->paginate($num);

        return $result;
    }


    //获取栏目下子栏目的数量
    public function getChildCountInPids($condition)
    {
        $where[] = ["pid", "in", $condition['pid']];
        $where[] = ["status", "<>", 99];

        $res = $this->where($where)
            ->field(["pid", "count(*) as count"])
            ->group("pid")
            ->select();
        // echo $this->getLastSql();exit();
        return $res;
    }


    /**
     * 根据PID 获取下级正常的分类数据
     * @param int $pid
     * @param $field 查询的字段
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getNormalByPid($pid = 0, $field)
    {
        $where = [
            "pid" => $pid,
            "status" => config("status.mysql.table_normal")
        ];

        $order = [
            "listorder" => "desc",
            "id" => "desc",
        ];
        $res = $this->where($where)
            ->field($field)
            ->order($order)
            ->select();

        return $res;
    }

    /**
     * 根据PID 获取上级正常的分类数据
     * @param int $pid
     * @param $field 查询的字段
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getNormalById($pid = 0, $field)
    {
        $where = [
            "id" => $pid,
            "status" => config("status.mysql.table_normal")
        ];

        $order = [
            "listorder" => "desc",
            "id" => "desc",
        ];
        $res = $this->where($where)
            ->field($field)
            ->order($order)
            ->select();

        return $res;
    }


    //根据category id 获取父ID
    public function getNormalCategoryId($id,$data, $filed = true)
    {
        $res = $this->where($id,'in',$data)
            ->field($filed)
            ->select();

       // dump($res->toArray());exit();
        return $res;

    }


}